'''
Created on 2014-5-3

@author: shen
'''
from matplotlib import numpy
from test.test_iterlen import len

class Classify_List:  
    '''
    the list to classify the list
    '''
#    list = []

    def __init__(self,the_list):
        self.list = the_list

    '''
        the number of groups
        '''
    def classify(self):
        now_s = 0
        pre_s = now_s
        max_s = 0
        
        list = self.list
        
        #divide into 50 groups
        num = 50
        count_list = [ 0 for i in range(num)]
        max_item = max(list)
        min_item = min(list)
        pace = (max_item-min_item)/num
        
        for item in list:
            for i in range(0,num):
                if item>pace*i+min_item and item<pace*(i+1)+min_item:
                    count_list[i] = count_list[i]+1
                
#        for i in range(0,num):
#            print str(min_item+pace*i)+" : "+str(count_list[i])
        
        current_list = []
        split_list = []
        
        for i in range(0,num):
            current_list.append(count_list[i])
#            print now_s
            pre_s = now_s
            
            if max(current_list)==0:
                interquatile_range_percent = 1      #the initialization of interquatile_range_percent 
                now_s = 0
            else:
                interquatile_range_percent = float(max(current_list)-count_list[i])/max(current_list)
                now_s = numpy.sqrt(numpy.var(current_list))/numpy.mean(current_list)
        
            if now_s == 0:
                pre_s = 1
            else:
                if now_s-max_s>0.0:
                    max_s = now_s
        
#            print str((max_s-now_s)/max_s)+" : "+str(now_s)
            
#            delta_2 = (max_s-now_s)/max_s
#            print delta_2
            if max(current_list)>0:
                print str(numpy.abs(pre_s - now_s)/pre_s)+" : "+str(interquatile_range_percent)
            
            
            if interquatile_range_percent>0.5 and (pre_s>0 and numpy.abs(pre_s - now_s)/pre_s<0.1)  :
                split_list.append(i)
                now_s = 0
                current_list = []
            
        
        price_list = []
        
        last_index = 0
        
        pre_index = 0
        for item in split_list:
            count = 0
            print str(pre_index) + ' '+ str(item)
            for i in range (pre_index , item):
                count = count+count_list[i]
            
            pre_index = item
            
            price_list.append({'range':min_item+pace*item , 'count':count})
            
            last_index = item
        
        count = 0
        for i in range (last_index , num):
                count = count+count_list[i]
        price_list.append({'range':max_item , 'count':count})
        
        #maintain the result
        a_list = []
        for item in price_list:
            a_list.append(item['count'])
        peak = max(a_list)
        valley = min(a_list)
        
        
        print price_list
        return price_list
        
        
        